///-----------------------------------------------------------------------------
/// Filename: XMLSubstituteController.cs
///
/// Description: 
/// This controller is supplies a single XML file to the caller. Before sending
/// to the client the XML file is searched for special parameter strings which
/// are replaced by the appropriate values.
/// 
/// This controller allows the delivery of things like a slightly customised XSL
/// file to a javascript call.
///
/// History:
/// 16 Aug 2006	Aaron Clauson	Created.
/// 
/// License: 
/// BSD
///-----------------------------------------------------------------------------

using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
using MaverickLite.Config;
using log4net;

namespace MaverickLite.Controller
{
	public class XMLSubstituteController : ControllerProduct
	{
		private const string XML_CONTENT_TYPE = "text/xml";
		private const string XML_FILEPATH_KEY = "path";

		protected string m_contentType = XML_CONTENT_TYPE;
		protected string m_output = null;

		/// <param name="initParams">The xml element containing the controller's intial parameters.</param>
		public XMLSubstituteController(XmlElement initParams)
		{			
			try
			{	
				// Clone the node containing the initial controller parameters, just in case
				// a derived controller wants to check or do some custom processing.
				InitialParameters = (XmlElement)initParams.Clone();

				#region Extract the childless node parameters into the simple parameters hashtable.

				// Load all the simple parameter nodes - those without children - into a hashtable.
				foreach(XmlNode parameterNode in initParams.SelectNodes(CONTROLLER_PARAM_KEY))
				{
					// Check if this is a childless (aka simple) parameter.
					if(parameterNode.ChildNodes.Count == 0)
					{
						string parameterName = parameterNode.Attributes.GetNamedItem(CONTROLLER_PARAM_NAME_ATTRIBUTE).Value;
						string parameterValue = parameterNode.Attributes.GetNamedItem(CONTROLLER_PARAM_VALUE_ATTRIBUTE).Value;

						// Insert name value pair into the parameters hashtable.
						if(parameterName != null && parameterName.Trim() != String.Empty)
						{
							this.SimpleParameters.Add(parameterName, parameterValue);
						}
					}		
				}

				#endregion
			}
			catch(Exception excp)
			{
				throw new ApplicationException("Exception XMLSubstituteController: " + excp.Message);
			}
		}

		
		public override void Go(HttpContext context)
		{
			context.Response.ContentType = m_contentType;

			string filePath = MavAppState.PhysicalApplicationPath + this.SimpleParameters[XML_FILEPATH_KEY];

			if(File.Exists(filePath))
			{
				XmlDocument xmlFile = new XmlDocument();
				xmlFile.Load(filePath);

				string model = xmlFile.OuterXml;

				foreach(DictionaryEntry specialParameter in MavAppState.SpecialParameters)
				{
					if(specialParameter.Value != null)
					{
						model = Regex.Replace(model, Regex.Escape((string)specialParameter.Key), (string)specialParameter.Value);
					}
				}

				context.Response.Write(model);
				context.Response.End();
			}
			
			logger.Error("The specified file, " + filePath + " does not exist in XMLSubstituteController.");

			throw new ApplicationException("The specified file does not exist in XMLSubstituteController.");
		}
	}
}
